// pages/deviceList/deviceList.js
var config = require("../config.js")

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: 0, // 给个默认值
    longitude: 0, // 给个默认值
    devicePileList: [
    ],
    totalPages: 0,
    curPage: 0,
    windowHeight: 0,
    userInfo: {},
    hidden:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
    this.setData({ windowHeight: wx.getSystemInfoSync().windowHeight })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getLocation({
      type: "gcj02", // 坐标系类型
      // 获取经纬度成功回调
      success: (res) => { // es6 箭头函数，可以解绑当前作用域的this指向，使得下面的this可以绑定到Page对象
        console.log("longtitude", res)
        this.setData({  // 为data对象里定义的经纬度默认值设置成获取到的真实经纬度，这样就可以在地图上显示我们的真实位置
          longitude: res.longitude,
          latitude: res.latitude
        })


        var longitude = that.data.longitude
        var latitude = that.data.latitude

        console.log("xxx. logi: ", that.data.longitude)

        // 获取附近的充电桩信息
        // wx.request({
        //   url: config.url_all + '/api/charge/pile/findByCoordinate',
        //   data: {
        //     longitude: longitude,
        //     latitude: latitude,
        //     range: 120000
        //   },
        //   header: {
        //     Rememberme: wx.getStorageSync('token')
        //   },
        //   method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        //   success: (res) => {
        //     console.log("find", res)
        //     that.setData({
        //       devicePileList: res.data.data
        //     })
        //   }
        // })
        wx.request({
          url: config.url_all + '/api/charge/devicePile/findPage',
          data: {
            pageable: {
              page: 0,
              size: 20
            }
          },
          header: {
            Rememberme: wx.getStorageSync('token')
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          success: (res) => {
            var content=res.data.data.content
            console.log("find", res)
            that.setData({
              devicePileList: res.data.data.content,
              totalPages: res.data.data.totalPages,
              curPage: res.data.data.number,
              hidden: (content.length > 0)
            })

          }
        })

      },
      fail: (res) => {
        wx.showToast({
          title: '获取GPS失败',
          duration: 4000
        });
        wx.navigateTo({
          url: '../myEquipment/myEquipment',
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 点击设备列表中的某一项
  getDeviceInfo: function (e) {
    var index = parseInt(e.currentTarget.dataset.index);

    var pileInfo = this.data.devicePileList[index];

    console.log("pileInfo: ", pileInfo)

    var curPileInfo = pileInfo;
    curPileInfo.curPileNew = false

    wx.setStorage({
      key: 'curPileInfo',
      data: curPileInfo,
    })

    wx.navigateTo({
      url: '../deviceinfo/deviceinfo'
    })
  },

  // 扫码添加设备
  scanCode: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        var mac = res.result
        // wx.setStorageSync('mac', mac)
        console.log(res)

        // get pile info by mac
        wx.request({
          url: config.url_all + '/api/charge/getDeviceInfo',
          data: {
            mac: mac.substr(0, 8) + '00',
          },
          header: {
            Rememberme: wx.getStorageSync('token')
          },
          method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          // header: {}, // 设置请求的 header
          success: (res) => {
            console.log("getDeviceInfo", res)
            console.log('lox', res.data.data)

            if ("OPERATE_FAILED" == res.data.code) {
              wx.showModal({
                content: res.data.message,
                showCancel: false
              })
            }
            else {
              var pileInfo = res.data.data;
              var curPileInfo = pileInfo;
              curPileInfo.longitude = that.data.longitude;
              curPileInfo.latitude = that.data.latitude;

              if (pileInfo.contact == "" || pileInfo.phone == "") {
                // app.globalData.curPileNew = true;
                curPileInfo.curPileNew = true

              }
              else {
                // app.globalData.curPileNew = false;
                curPileInfo.curPileNew = false
              }

              wx.setStorage({
                key: 'curPileInfo',
                data: curPileInfo,
              })

              wx.navigateTo({
                url: '../deviceInfo/deviceInfo'
              })
            }


          }
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  searchScrollLower: function () {
    var that = this;
    if (that.data.curPage + 1 >= that.data.totalPages) {
      return;
    }

    console.log("curPage: ", that.data.curPage)
    console.log("totalPages: ", that.data.totalPages)


    wx.showLoading({
      title: '正在加载',
    })

    wx.request({
      url: config.url_all + '/api/charge/devicePile/findPage',
      data: {
        pageable: {
          page: that.data.curPage + 1,
          size: 20
        }
      },
      header: {
        Rememberme: wx.getStorageSync('token')
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        var _data = res.data.data;
        var _devicePileList = that.data.devicePileList;
        var content=res.data.data.content

        console.log("res.data:  ", res.data)

        for (var i in _data.content) {
          _devicePileList.push(_data.content[i])
        }

        that.setData({
          devicePileList: _devicePileList,
          curPage: _data.number,
          totalPages: _data.totalPages,
          hidden: (content.length > 0)
        })

        console.log("size: ", _devicePileList.length)
        wx.hideLoading();
      }
    })
  }
})